﻿using OABackground.Entities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace OABackground.Infrastructure
{
    public class WageRepository
    {
        public void Add(string teacherid, string wagetime, float basicwage, float? overtimewage, float? welfare, float? bonus)
        {
            using (oaContext context = new oaContext())
            {
                DateTime dt = Convert.ToDateTime(wagetime);
                Wage wage = new Wage
                {
                    Teacherid = teacherid,
                    Wagetime = dt,
                    Basicwage = basicwage,
                    Overtimewage = overtimewage,
                    Welfare = welfare,
                    Bonus = bonus,
                    Totalwage = basicwage + overtimewage + welfare + bonus
                };
                context.Wage.Add(wage);
                context.SaveChanges();
            }
        }
        public void Add(string teacherid, string wagetime, float basicwage)
        {
            using (oaContext context = new oaContext())
            {
                DateTime dt = Convert.ToDateTime(wagetime);
                Wage wage = new Wage
                {
                    Teacherid = teacherid,
                    Wagetime = dt,
                    Basicwage = basicwage,
                    Totalwage = basicwage
                };
                context.Wage.Add(wage);
                context.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            using (oaContext context = new oaContext())
            {
                var wage = context.Wage.FirstOrDefault(x => x.Id == id);
                context.Wage.Remove(wage);
                context.SaveChanges();
            }
        }
        public Wage QuerybyID(int id)
        {
            using (var context = new oaContext())
            {
                var wage = context.Wage.FirstOrDefault(x => x.Id == id);
                context.SaveChanges();
                //wage.Wagetime = wage.Wagetime.ToString("yyyy-MM");
                return wage;
            }
        }
        public List<Wage> QueryAllbyTidYear(string teacherid, string year)
        {
            List<Wage> wageList = new List<Wage>();
            List<Wagestringtime> wageListstring = new List<Wagestringtime>();
            string Jan = "-01-01";
            string Dec = "-12-01";
            string Begin = year + Jan;
            string End = year + Dec;
            DateTime BeginTime = Convert.ToDateTime(Begin);
            DateTime EndTime = Convert.ToDateTime(End);
            using (var context = new oaContext())
            {
                wageList = (from u in context.Wage
                            where u.Teacherid.Equals(teacherid) && DateTime.Compare(u.Wagetime, BeginTime) >= 0 && DateTime.Compare(u.Wagetime, EndTime) <= 0
                            select u
                                ).ToList();
                context.SaveChanges();
            }
            return wageList;
        }
        public Wage QuerybyTidMonth(string teacherid, string time)
        {

            Wage wage = new Wage();
            DateTime dt = Convert.ToDateTime(time);
            using (var context = new oaContext())
            {
                wage = (from u in context.Wage
                        where (0 == DateTime.Compare(u.Wagetime, dt)) && (u.Teacherid.Equals(teacherid))
                        select u
                            ).FirstOrDefault();
                context.SaveChanges();
            }
            return wage;
        }
        public List<Wage> QuerybyTid(string teacherid)
        {
            List<Wage> wageList = new List<Wage>();
            using (var context = new oaContext())
            {
                wageList = (from u in context.Wage
                            where u.Teacherid.Equals(teacherid)
                            select u
                            ).ToList(); ;
                context.SaveChanges();
            }
            return wageList;
        }
        public List<Wage> QueryTAll()
        {
            List<Wage> wageList = new List<Wage>();
            using (var context = new oaContext())
            {
                wageList = (from u in context.Wage
                            select u
                            ).ToList(); ;
                context.SaveChanges();
            }
            return wageList;
        }
        public List<Wage> QueryAllbyDidMonth(string departmentid, string time)
        {
            List<Wage> wageList = new List<Wage>();
            DateTime Time = Convert.ToDateTime(time);
            using (var context = new oaContext())
            {
                wageList = (from u in context.Wage
                            from v in context.Teacherinfomation
                            where u.Teacherid.Equals(v.Teacherid) && v.Departmentid.Equals(departmentid) && DateTime.Compare(u.Wagetime, Time) == 0
                            select u
                                ).ToList();
                context.SaveChanges();
            }
            return wageList;
        }
        public Object QueryAllbyDidMonth2(string departmentid, string time)
        {
            DateTime Time = Convert.ToDateTime(time);
            using (var context = new oaContext())
            {
                var list = (from u in context.Wage
                            from v in context.Teacherinfomation
                            where u.Teacherid.Equals(v.Teacherid) && v.Departmentid.Equals(departmentid) && DateTime.Compare(u.Wagetime, Time) == 0
                            select new { u.Id, u.Teacherid, u.Wagetime, u.Basicwage, u.Overtimewage, u.Welfare, u.Bonus, u.Totalwage, v.Tname }
                                ).ToList();
                context.SaveChanges();
                return list;
            }
        }
        public List<Wage> QueryAllbyDid(string departmentid)
        {
            List<Wage> wageList = new List<Wage>();
            using (var context = new oaContext())
            {
                wageList = (from u in context.Wage
                            from v in context.Teacherinfomation
                            where u.Teacherid.Equals(v.Teacherid) && v.Departmentid.Equals(departmentid)
                            select u
                                ).ToList();
                context.SaveChanges();
            }
            return wageList;
        }
        public Object QueryAllbyDid2(string departmentid)
        {
            using (var context = new oaContext())
            {
                var list = (from u in context.Wage
                            from v in context.Teacherinfomation
                            where u.Teacherid.Equals(v.Teacherid) && v.Departmentid.Equals(departmentid)
                            select new { u.Id, u.Teacherid, u.Wagetime, u.Basicwage, u.Overtimewage, u.Welfare, u.Bonus, u.Totalwage, v.Tname }
                                ).ToList();
                context.SaveChanges();
                return list;
            }
        }


        public List<Wage> QuerybyTidAllMonth(string teacherid, string month)   /*-----------id+y月*/
        {

            List<Wage> wageList = new List<Wage>();
            month = month.Replace("-", "");
            using (var context = new oaContext())
            {
                wageList = (from u in context.Wage
                            where (u.Wagetime.Month == int.Parse(month)) && (u.Teacherid.Equals(teacherid))
                            select u
                            ).ToList();
                context.SaveChanges();
            }
            return wageList;
        }


        public void Update(Wage wage)
        {
            using (var context = new oaContext())
            {
                context.Wage.Update(wage);
                context.SaveChanges();
            }
        }
        public List<Wagestringtime> FixTime(List<Wage> wageList)
        {
            List<Wagestringtime> wagestringtimeList = new List<Wagestringtime>();
            foreach (var u in wageList)
            {
                wagestringtimeList.Add(new Wagestringtime { Id = u.Id, Teacherid = u.Teacherid, Wagetime = u.Wagetime.ToString("yyyy-MM"), Basicwage = u.Basicwage, Overtimewage = u.Overtimewage, Welfare = u.Welfare, Bonus = u.Bonus, Totalwage = u.Totalwage });
            }
            return wagestringtimeList;
        }
    }

    public class Wagestringtime
    {
        public int Id { get; set; }
        public string Teacherid { get; set; }
        public string Wagetime { get; set; }
        public float Basicwage { get; set; }
        public float? Overtimewage { get; set; }
        public float? Welfare { get; set; }
        public float? Bonus { get; set; }
        public float? Totalwage { get; set; }
    }
}
